<?php
/**
 * Created by : PhpStorm
 * Index: 阿城 <479468212@qq.com>
 * Date: 2019/12/27
 * Time: 13:02
 */

namespace app\api\model;

use app\common\model\Base;

class MessageLog extends Base {
    protected $autoWriteTimestamp = true;
    public static $typeArr;
    public static $statusArr;
    // 追加属性
    protected $append = ['type_text','status_text'];

    // 模型初始化
    protected static function init()
    {
        self::$typeArr = get_data_dict('message_type');
        self::$statusArr = get_data_dict('message_status');
    }

    public static function sendMessage($to, $content, $type = 1, $sendUid = 0)
    {
        if(is_array($to)){
            $data = [];
            foreach ($to as $uid){
                $data[] = [
                    'uid'=>$uid,
                    'send_uid'=>$sendUid,
                    'type'=>$type,
                    'content'=>$content,
                ];
            }
            $result = self::saveAll($data);
        }else{
            $result = self::create([
                'uid'=>$to,
                'send_uid'=>$sendUid,
                'type'=>$type,
                'content'=>$content,
            ]);
        }
        return $result;
    }

    public function getTypeTextAttr($value,$data)
    {
        if(!strlen($data['type'])){
            return '';
        }
        return self::$typeArr[$data['type']];
    }

    public function getStatusTextAttr($value,$data)
    {
        if(!strlen($data['status'])){
            return '';
        }
        return self::$statusArr[$data['status']];
    }

}
